﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Text;

namespace nwrole.lib {
    

    /*[Serializable]
    internal class DomainResolver {
        public string PluginDirectory { get; set; }

        public Assembly AssemblyResolveEvent(object sender, ResolveEventArgs args) {
            EventLogger.LogInformation("DomainResolver.AssemblyResolveEvent", args.Name);
            string name = args.Name;
            if (name.Contains(','))
                name = name.Split(',').First().Trim();
            if (!name.EndsWith(".dll"))
                name += ".dll";
            return Assembly.LoadFile(Path.Combine(PluginDirectory, name));
        }
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Plugin : IServiceContract {
        public enum PluginState {
            Running,
            Halted
        }

        public PluginData PluginData { get; private set; }
        public PluginState State { get; private set; }

        public Assembly Assembly { get; private set; }
        public Type PluginType { get; private set; }
        public PluginBase Object { get; private set; }

        public Plugin(PluginData pluginData) {
            PluginData = pluginData;
        }

        // verifierar att servicemanagern är startad och svarar med rätt värden
        public Guid GetIdentifier() {
            EventLogger.LogInformation(String.Format("Plugin.GetIdentifier ({0})", PluginData.Id), "Providing plugin identifier.");
            return PluginData.Id;
        }

        public Result Start() {
            try {
                EventLogger.LogInformation(String.Format("Plugin.Start ({0})", PluginData.Id), "Starting instance");

                DomainResolver resolver = new DomainResolver {
                    PluginDirectory = PluginDirectoryPath(PluginData),
                };

                AppDomain domain = AppDomain.CurrentDomain;
                domain.SetupInformation.ApplicationBase = PluginDirectoryPath(PluginData); //InstanceDirectoryPath(instance);
                domain.AssemblyResolve += new ResolveEventHandler(resolver.AssemblyResolveEvent);

                Object = (PluginBase)domain.CreateInstanceAndUnwrap(PluginData.Assembly, PluginData.ClassName);
                Object.Initialize(PluginData);

                Object.Start();

                State = PluginState.Running;

                EventLogger.LogInformation(String.Format("Plugin.Start ({0})", PluginData.Id), "Started instance");
            } catch (Exception exception) {
                EventLogger.LogException(String.Format("Plugin.Start ({0})", PluginData.Id), exception);
                State = PluginState.Halted;
                Object = null;
                return Result.UnknownError;
            }
            return Result.Success;
        }

        public Result Stop() {
            try {
                EventLogger.LogInformation(String.Format("Plugin.Stop ({0})", PluginData.Id), "Stopping instance");

                State = PluginState.Halted;
                Object.Stop();
                Object = null;

                EventLogger.LogInformation(String.Format("Plugin.Stop ({0})", PluginData.Id),"Stopped instance");
            } catch (Exception exception) {
                EventLogger.LogException(String.Format("Plugin.Stop ({0})", PluginData.Id), exception);
                Object = null;
                return Result.UnknownError;
            }

            return Result.Success;
        }

        public static string PluginDirectoryPath(PluginData pluginData) {
            string path = Path.Combine(Configuration.PluginsDirectory, pluginData.Id.ToString("N"));
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }
    }*/
}
